package com.nimitz.module.employee.service.impl;

import com.nimitz.common.annotation.DataScope;
import com.nimitz.common.utils.SecurityUtils;
import com.nimitz.module.employee.domain.EmpEmployee;
import com.nimitz.module.employee.domain.EmpPost;
import com.nimitz.module.employee.mapper.EmpEmployeeMapper;
import com.nimitz.module.employee.mapper.EmpPostMapper;
import com.nimitz.module.employee.service.IEmpPostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 转岗 业务层实现
 */
@Service
public class EmpPostServiceImpl implements IEmpPostService {

    @Resource
    private EmpPostMapper postMapper;
    @Resource
    private EmpEmployeeMapper employeeMapper;

    /**
     * 新增转岗
     *
     * @param post 转岗
     * @return
     */
    @Override
    public int insertPost(EmpPost post) {
        return postMapper.insertPost(post);
    }

    /**
     * 批量删除转岗
     *
     * @param postIds 人员id集合
     * @return 结果
     */
    public int deletePostByIds(String updateBy, Integer[] postIds) {
        return postMapper.deletePostByIds(updateBy, postIds);
    }

    /**
     * 修改转岗
     *
     * @param post 转岗
     * @return
     */
    @Override
    public int updatePost(EmpPost post) {
        return postMapper.updatePost(post);
    }

    /**
     * 根据转岗id查询转岗
     *
     * @param postId 转岗ID
     * @return
     */
    @Override
    public EmpPost selectPostById(Integer postId) {
        return postMapper.selectPostById(postId);
    }

    /**
     * 根据条件查询转岗列表（含分页）
     *
     * @param post 转岗
     * @return
     */
    @Override
    @DataScope(deptAlias = "tn1")
    public List<EmpPost> selectPostList(EmpPost post) {
        return postMapper.selectPostList(post);
    }

    /**
     * 批量提交
     *
     * @param updateBy 更新者
     * @param postIds  转岗ID集合
     * @return
     */
    @Override
    public int submitPost(String updateBy, Integer[] postIds) {
        return postMapper.submitPost(updateBy, postIds);
    }

    /**
     * 批量驳回
     *
     * @param updateBy 更新者
     * @param postIds  转岗ID集合
     * @return
     */
    @Override
    public int rejectPost(String updateBy, Integer[] postIds) {
        return postMapper.rejectPost(updateBy, postIds);
    }

    /**
     * 批量审核
     *
     * @param updateBy 更新者
     * @param postIds  转岗ID集合
     * @return
     */
    @Override
    @Transactional
    public int auditPost(String updateBy, Integer[] postIds) {
        int returnVal =postMapper.auditPost(updateBy, postIds);
        if (returnVal > 0) {
            // 根据postIds获取集合
            List<EmpPost> postList = postMapper.selectPostByIds(postIds);
            // 生成人员记录集合
            List<EmpEmployee> employeeList = new ArrayList<>();
            for (EmpPost post : postList){
                EmpEmployee employee = new EmpEmployee();
                employee.setEmployeeId(post.getEmployeeId());
                employee.setDeptId(post.getPostExchangeDeptId());
                employee.setEmployeeConfirmationDate(post.getPostEmployee().getEmployeeConfirmationDate());
                employee.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
                employeeList.add(employee);
            }
            // 批量修改人员转岗部门
            employeeMapper.updateEmployeeByList(employeeList);
        }
        return returnVal;
    }
}
